<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="livcos.org/ns/xslt/function"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:pm="livcos.org/ns/pm"
	xmlns="http://www.w3.org/1999/XSL/Format"
	exclude-result-prefixes="doc pm f xs">
	
  <xsl:param name="incl" select="-1" as="xs:integer"/>
  <xsl:param name="excl" select="-1" as="xs:integer"/>
	
	<xsl:variable name="dayWidth" select="0.2"/>
  
  <xsl:function name="f:sort">
    <xsl:param name="in" />
    <xsl:perform-sort select="$in">
      <xsl:sort select="." />
    </xsl:perform-sort>
  </xsl:function>

  <xsl:function name="f:countMonths" as="xs:integer">
    <xsl:param name="date" />
    <xsl:sequence select="month-from-date($date) + (year-from-date($date) * 12)"/>
  </xsl:function>

	<xsl:variable name="root"
	  select="document('../../../data/pm/Roadmap.view')/*" />
  <xsl:variable name="apps" as="node()*">
    <xsl:choose>
      <xsl:when test="$incl > 0">
        <xsl:sequence select="$root//pm:application[position() = $incl]"/>
      </xsl:when>
      <xsl:when test="$excl > 0">
        <xsl:sequence select="$root//pm:application[position() != $excl]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="$root//pm:application"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="first" select="xs:date(f:sort($apps/pm:release/@startDate)[1])" />
	<xsl:variable name="last" select="xs:date(f:sort($apps/pm:release/@releaseDate)[last()])" />
  <xsl:variable name="months" as="xs:date*">
    <xsl:for-each select="0 to (f:countMonths($last) - f:countMonths($first))">
      <xsl:sequence select="$first + (xs:yearMonthDuration('P1M') * xs:double(.))"/>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="days" select="days-from-duration($last - $first)" />

	<xsl:template match="/">
	  <xsl:apply-templates select="$root" />
	</xsl:template>
	
  <xsl:attribute-set name="border">
    <xsl:attribute name="border-width">thin</xsl:attribute>
    <xsl:attribute name="border-color">#AAAAAA</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="border-top" use-attribute-sets="border">
    <xsl:attribute name="border-top-style">dashed</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="border-left" use-attribute-sets="border">
    <xsl:attribute name="border-left-style">dashed</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:template match="/*">
    <xsl:variable name="numCols" select="count($months)"/>
    <table table-layout="fixed" width="100%" margin-left="0.6cm" font-size="7pt">
      <table-column column-width="18%"/>
      <table-column column-width="82%"/>
      <table-header font-weight="normal">
        <table-row>
          <table-cell padding="0pt">
            <block>&#160;</block>
            <block>&#160;</block>
          </table-cell>
          <table-cell padding="0pt" margin="0pt">
            <xsl:for-each select="$months">
              <xsl:variable name="d" select="days-from-duration(. - $first)"/>
              <block-container left="{$d * $dayWidth}mm" absolute-position="absolute">
                <xsl:choose>
                  <xsl:when test="(position() = 1) or (month-from-date(.) = 1)">
                    <block xsl:use-attribute-sets="border-left" padding-left="1pt">
                      <xsl:value-of select="format-number(year-from-date(.), '0000')"/>
                    </block>
                  </xsl:when>
                  <xsl:otherwise>
                    <block>&#160;</block>
                  </xsl:otherwise>
                </xsl:choose>
                <block xsl:use-attribute-sets="border-left" padding-left="1pt">
                  <xsl:value-of select="format-number(month-from-date(.), '00')"/>
                </block>
              </block-container>
            </xsl:for-each>
          </table-cell>
        </table-row>
      </table-header>
      <table-body>
        <xsl:apply-templates select="$apps" />
        <table-row>
          <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
            <block>&#160;</block>
          </table-cell>
          <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
            <block>&#160;</block>
          </table-cell>
        </table-row>
      </table-body>
    </table>
  </xsl:template>
  
  <xsl:template match="pm:application">
    <table-row>
      <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
        <block margin-left="0pt" padding="0pt">
          <xsl:value-of select="doc:title" />
        </block>
      </table-cell>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="2pt">
        <block margin-left="0pt" text-align="center">
        </block>
      </table-cell>
    </table-row>
    <xsl:apply-templates select="pm:release" />
  </xsl:template>
  
  <xsl:template match="pm:release">
    <table-row>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="2pt">
        <block margin-left="4pt">
          <xsl:value-of select="@num"/>
          <xsl:if test="@label">
            <xsl:text> </xsl:text>
            <xsl:value-of select="@label"/>
          </xsl:if>
        </block>
      </table-cell>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="0pt">
        <xsl:variable name="s" select="days-from-duration(xs:date(@startDate) - $first)"/>
        <xsl:variable name="d" select="days-from-duration(xs:date(@releaseDate) - xs:date(@startDate))"/>
        <xsl:choose>
        	<xsl:when test="not(@startDate and @releaseDate)">
        		<block text-align="right" padding-right="1pt" font-size="6pt" color="white">
        		</block>
        	</xsl:when>
        	<xsl:when test="$d &gt; 60">
						<block-container left="{$s * $dayWidth}mm" top="2pt" absolute-position="absolute" width="{$d * $dayWidth}mm"
							background-color="#6666BB">
							<block text-align="right" padding-right="1pt" font-size="6pt" color="white">
								<xsl:value-of select="@releaseDate"/>
							</block>
						</block-container>
        	</xsl:when>
        	<xsl:otherwise>
						<block-container left="{$s * $dayWidth}mm" top="2pt" absolute-position="absolute" width="{$d * $dayWidth}mm"
							background-color="#6666BB">
							<block text-align="left" padding-left="1pt" font-size="6pt" margin-left="{$d * $dayWidth}mm">
								<xsl:value-of select="@releaseDate"/>
							</block>
						</block-container>
          </xsl:otherwise>
        </xsl:choose>
      </table-cell>
    </table-row>
  </xsl:template>
  
</xsl:stylesheet>
